﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SAP.People
{
    class Spider
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private SpiderContext context = new SpiderContext();

        public static Spider Create()
        {
            log.Info("Create spider");
            return new Spider();
        }

        public void Run()
        {
            log.Info("Start to retrieve data");

            try
            {
                var tasks = new ITask[] 
                { 
                    new InitTask(context), 
                    new RestoreTask(context), 
                    new CrawlTask(context), 
                    new MergeTask(context) 
                };

                foreach (var task in tasks)
                {
                    task.RunSynchronously();
                }
            }
            catch (Exception e)
            {
                log.Error("Error", e);
            }

            log.Info("End of task");
        }
    }
}
